package fetcher

import (
	"net/http"
	"fmt"
	"golang.org/x/text/transform"
	"io/ioutil"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"bufio"
	"golang.org/x/net/html/charset"
	"log"
)


type Mv struct {
	Directors []string `json:"directors"`
	Rate      string   `json:"rate"`
	CoverX   int64   `json:"cover_x"`
	Star      string   `json:"star"`
	Title     string   `json:"title"`
	Url       string   `json:"url"`
	Casts     []string `json:"casts"`
	Cover     string   `json:"cover"`
	Id        string   `json:"id"`
	CoverY   int64   `json:"cover_y"`
}
type MyDatas struct {
	Data []Mv 	`json:"data"`
}
func Fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
	}
	bodyReader := bufio.NewReader(resp.Body)
	e := determineEncoding(bodyReader)
	utf8Reader := transform.NewReader(bodyReader, e.NewDecoder())
	return ioutil.ReadAll(utf8Reader)

}

func determineEncoding(r *bufio.Reader) encoding.Encoding {
	bytes, err := r.Peek(1024)
	if err != nil {
		log.Printf("Fetcher error: %v", err)
		return unicode.UTF8
	}
	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}


